<?php
/**
 * Clean up the_excerpt()
 */
function roots_excerpt_more($more) {
  return ' &hellip; <a href="' . get_permalink() . '">' . __('Continued', 'roots') . '</a>';
}
add_filter('excerpt_more', 'roots_excerpt_more');

/**
 * Manage output of wp_title()
 */
function roots_wp_title($title) {
  if (is_feed()) {
    return $title;
  }

  $title .= get_bloginfo('name');

  return $title;
}
add_filter('wp_title', 'roots_wp_title', 10);
if ( ! function_exists( 'joke_paging_nav' ) ) :
function joke_paging_nav() {
    if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
        return;
    }

    $paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
    $pagenum_link = html_entity_decode( get_pagenum_link() );
    $query_args   = array();
    $url_parts    = explode( '?', $pagenum_link );

    if ( isset( $url_parts[1] ) ) {
        wp_parse_str( $url_parts[1], $query_args );
    }

    $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
    $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

    $format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
    $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';

    // Set up paginated links.
    $links = paginate_links( array(
        'base'     => $pagenum_link,
        'format'   => $format,
        'total'    => $GLOBALS['wp_query']->max_num_pages,
        'current'  => $paged,
        'mid_size' => 1,
        'type'=>'list',
        'add_args' => array_map( 'urlencode', $query_args ),
        'prev_text' => '上一页',
        'next_text' => '下一页',
    ) );

    if ( $links ) :

    ?>
    <nav class="navigation paging-navigation pure-u-1" role="navigation">
        <?php echo $links; ?>
    </nav>
    <?php
    endif;
}
endif;
function get_jokes(){
    global $wpdb;
    $last=0;
    if(isset($_GET['last'])&&!empty($_GET['last'])){
        $last=absint($_GET['last']);
    }
    $page=1;
    if(isset($_GET['page'])&&!empty($_GET['page'])){
        $page=absint($_GET['page']);
    }
    $total_sql="select count(1) as total from $wpdb->posts where post_type='joke' and post_status='publish' ";
    if($last>0){
        $total_sql=$total_sql.' and ID<=%d';
        $total=intval($wpdb->get_var($wpdb->prepare($total_sql,$last)));
    }else{
        $total=intval($wpdb->get_var($total_sql));
    }
    $size=get_option('posts_per_page',20);
    $total_pages=ceil(($total*1.0)/$size);

    $page=min($total_pages,$page);
    $offset=($page-1)*$size;
    if($last>0){
        $results=$wpdb->get_results($wpdb->prepare("select a.ID,a.post_content,a.post_date_gmt as post_date,a.post_author from $wpdb->posts a where a.post_type='joke' and a.post_status='publish' and a.ID <= %d order by a.ID desc limit ".$offset.','.$size,$last));
    }else{
        $results=$wpdb->get_results("select a.ID,a.post_content,a.post_date_gmt as post_date,a.post_author from $wpdb->posts a where a.post_type='joke' and a.post_status='publish' order by a.ID desc limit $offset,$size");
    }
    foreach($results as $result){
        $result->url=get_permalink($result->ID);
        $result->author_name=$wpdb->get_var($wpdb->prepare("select meta_value from $wpdb->usermeta where user_id=%d and meta_key='nickname'",$result->post_author));
        $result->author_avatar=$wpdb->get_var($wpdb->prepare("select meta_value from $wpdb->usermeta where user_id=%d and meta_key='avatar'",$result->post_author));
        $result->author_url=get_author_posts_url($result->post_author);
        $result->comments_count=$wpdb->get_var($wpdb->prepare("select count(1) from $wpdb->comments where comment_post_ID=%d",$result->ID));
        $result->post_date=date_i18n('c',strtotime($result->post_date));
    }
    $model=array('items'=>$results,'next'=>$page+1,'hasNext'=>$page<$total_pages);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($model);
    exit;
}

add_action('wp_ajax_get_jokes','get_jokes');
add_action('wp_ajax_nopriv_get_jokes','get_jokes');
add_filter('roots/wrap_base', 'roots_wrap_base_cpts'); // Add our function to the roots_wrap_base filter

function roots_wrap_base_cpts($templates) {
    $cpt = get_post_type(); // Get the current post type
    if ($cpt) {
       array_unshift($templates, 'base-' . $cpt . '.php'); // Shift the template to the front of the array
    }
    return $templates; // Return our modified array with base-$cpt.php at the front of the queue
}
